package cn.brilliantfuture.commentry.util;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 
 * Title: Cabinet Platform
 * 
 * Author: LiTian
 * 
 * Date: 2012-7-27
 * 
 * Description:
 * 
 */
public class SMSProfile implements Serializable {

	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger(SMSProfile.class);

	private static SMSProfile instance = null;

	private Properties properties = new Properties();

	private SMSProfile() {

	}

	public synchronized static SMSProfile getInstance() {
		if (instance == null) {
			instance = new SMSProfile();
			instance.load();
		}
		return instance;
	}

	private void load() {
		logger.info("Loading sms data ...");

		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;

		properties.clear();

		try {
			conn = JDBCDataSource.getConnection();
			pstm = conn.prepareStatement("select language_info_id, sms_use, apply_orient, sms_content from sms_info where deleted = 0");
			rs = pstm.executeQuery();

			while (rs.next()) {
				String key = String.valueOf(rs.getInt("language_info_id")) + String.valueOf(rs.getInt("sms_use")) + String.valueOf(rs.getInt("apply_orient"));
				String value = rs.getString("sms_content");
				properties.setProperty(key, value);
			}

		} catch (Exception e) {
			logger.info("load sms data fail ...\n" + e.getMessage());
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (pstm != null)
					pstm.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void reload() {
		logger.info("Reload sms data ...");
		load();
	}

	public String get(String key) {
		return properties.getProperty(key);
	}
}
